package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.ClassifyDetailsAdapter;
import com.zw.yzk.kabu.adapter.ClassifyShopAdapter;
import com.zw.yzk.kabu.adapter.GoodsClassifyAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerGoodsClassifyComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.GoodsClassifyModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.ClassifyDetailsModel;
import com.zw.yzk.kabu.model.ClassifyShopModel;
import com.zw.yzk.kabu.model.GoodsClassifyModel;
import com.zw.yzk.kabu.presenter.GoodsClassifyPresenter;
import com.zw.yzk.kabu.view.view.GoodsClassifyView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class GoodsClassifyActivity extends BaseActivity implements GoodsClassifyView, ClassifyDetailsAdapter.OnClassifyItemClickedListener {

    @Bind(R.id.goods_classify)
    RecyclerView goodsClassify;
    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.shop_list)
    RecyclerView shopList;
    @Bind(R.id.empty)
    View empty;
    private RecyclerView shopListHeader;
    private int currentClassifyType = -1;

    @Inject
    GoodsClassifyPresenter presenter;
    private GoodsClassifyAdapter classifyAdapter;
    private ClassifyDetailsAdapter detailsAdapter;
    private ClassifyShopAdapter shopAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.actiivity_goods_classify;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.total_classify))
                .build();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                initRecyclerView();
                getGoodsClassify();
            }
        });

        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void initComponent() {
        DaggerGoodsClassifyComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .goodsClassifyModule(new GoodsClassifyModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    public void getGoodsClassifySuccess(List<GoodsClassifyModel> list) {
        refreshList(list);
    }

    @Override
    public void getClassifyShopSuccess(List<ClassifyShopModel> shopList, List<ClassifyDetailsModel> detailsList) {
        if (refreshLayout.isLoading()) {
            shopAdapter.add(shopList);
            refreshLayout.loadMoreFinish(shopList.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            shopAdapter.refresh(shopList);
            detailsAdapter.refresh(detailsList);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }

    }

    @Override
    public void getClassifyShopFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    @Override
    public void onItemClicked(ClassifyDetailsModel model) {
        Intent intent = new Intent(this, SubClassifyActivity.class);
        intent.putExtra(ActivityConstant.GOODS_CLASSIFY_TITLE, model.getName());
        intent.putExtra(ActivityConstant.GOODS_CLASSIFY_TYPE, model.getId());
        startActivity(intent);
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        goodsClassify.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        shopList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        shopListHeader = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.recycler_header_classify_shop_list, null);
        shopListHeader.setLayoutManager(new GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false));
        shopAdapter = new ClassifyShopAdapter(null, shopList.getWidth());
        shopList.setAdapter(shopAdapter);
        detailsAdapter = new ClassifyDetailsAdapter(null);
        detailsAdapter.setOnClassifyItemClickedListener(this);
        shopListHeader.setAdapter(detailsAdapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setHeader(shopListHeader)
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getClassifyShop(0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getClassifyShop(shopAdapter.getItemCount());
                    }
                }).build();
    }

    /**
     * 刷新列表
     *
     * @param list
     */
    private void refreshList(List<GoodsClassifyModel> list) {
        if (classifyAdapter == null) {
            classifyAdapter = new GoodsClassifyAdapter(list);
            goodsClassify.setAdapter(classifyAdapter);
            for (GoodsClassifyModel model : list) {
                if (model.isSelected() && currentClassifyType == -1) {
                    currentClassifyType = model.getId();
                    //获取详细分类
                    getClassifyShop(0);
                    break;
                }
            }
            classifyAdapter.setOnClassifyClickedListener(new GoodsClassifyAdapter.OnClassifyClickedListener() {
                @Override
                public void onClassifyClicker(int typeId) {
                    currentClassifyType = typeId;
                    //获取详细分类
                    getClassifyShop(0);
                }
            });
        } else {
            classifyAdapter.refresh(list);
        }

        refreshEmptyStatus(classifyAdapter.getItemCount() == 0);
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    /**
     * 获取商品分类
     */
    private void getGoodsClassify() {
        presenter.getGoodsClassify();
    }

    /**
     * 获取商品二级分类
     */
    private void getClassifyShop(int start) {
        presenter.getClassifyShop(start, currentClassifyType);
    }
}
